// pages/user/personal_code/personal_code.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        url: '',
        canvasUrl: '',
        userInfo: '',
        isReady: false,
        isAuthSetting:false,
        userId: ''

    },
    // ctx:null

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            userId: options.userId
        });
        this.getCode();
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    // 分享
    onShareAppMessage(res) {
        return {
            title: '广州大学校友会校友企业家专委会',
            path: '/pages/index/index'
        }
    },
    // 获取信息
    getCode: function() {
        let _this = this;
        app.util.ajax({
            url: 'user/qrcode',
            data: {
                // user_id: app.globalData.userInfo.user_id,
                user_id: _this.data.userId,
                // scene:'id='+app.globalData.userInfo.user_id+'&state='+app.globalData.userInfo.state,
                scene:'id='+_this.data.userId+'&state='+app.globalData.userInfo.state,
                url:'pages/user/personal/personal'
            },
            success: function(res) {
                let data = res.company || res.enterprise;
                data.nickname = data.nickname ? data.nickname : data.name;
                _this.setData({
                    url: data.qrcode,
                    userInfo: data
                })
                _this.drawUserCode();
            }
        })
    },
    // 绘制二维码
    drawUserCode: function() {
        let info = this.data.userInfo,
            url = info.images,
            codeUrl = info.qrcode,
            _this = this;
        const ctx = wx.createCanvasContext('canvas');
        // 头像
        wx.downloadFile({
            url: url,
            success: function(res) {
                let fileUrl = res.tempFilePath;
                wx.getImageInfo({
                    src: fileUrl,
                    success: function(data) {
                        let drawImageUrl = data.path;
                        ctx.save();
                        ctx.arc(45, 45, 35, Math.PI * 2, false);
                        ctx.clip();
                        if (drawImageUrl != '') {
                            ctx.drawImage(drawImageUrl, 10, 10, 70, 70);
                        }
                        ctx.restore();
                        ctx.draw(true, function() {
                            _this.setData({
                                isReady: true
                            })
                        });
                    }
                })
            }
        });
        // 二维码
        wx.downloadFile({
            url: codeUrl,
            success: function(res) {
                let fileUrl = res.tempFilePath;
                wx.getImageInfo({
                    src: fileUrl,
                    success: function(data) {
                        let drawImageUrl = data.path;
                        ctx.setFontSize(18);
                        if (info.state == 2) {
                            ctx.fillText(info.name, 91, 35);
                        } else {
                            ctx.fillText(info.nickname, 91, 30);
                        }
                        ctx.setFontSize(14);
                        ctx.setFillStyle('#999999');
                        if (info.state == '1') {
                            let userName = info.company.length>0 ? info.company[0].name : '暂无公司';
                            // userName = info.company.length > 1 ? userName +'等等' : userName;
                            userName = info.company.length > 1 ? userName : userName;
                            ctx.fillText(info.position, 90, 50);
                            ctx.fillText(userName, 90, 70);
                        } else {
                            ctx.fillText(info.key_word, 90, 65);
                        }
                        ctx.fillText('使用智能二维码 方可扫描进入您的名片', 40, 400);
                        ctx.draw(true);
                        ctx.restore();
                        ctx.drawImage(drawImageUrl, 45, 106, 255, 255);
                        ctx.draw(true, function() {
                            _this.setData({
                                isReady: true
                            })
                        });
                    }
                })
            }
        });
    },
    // 保存图片
    saveCode: function() {
        let _this = this;
        wx.showLoading();
        wx.canvasToTempFilePath({
            canvasId: 'canvas',
            x: 0,
            y: 0,
            width: 345,
            height: 434,
            fileType: 'jpg',
            success: function(res) {
                let url = res.tempFilePath;
                wx.saveImageToPhotosAlbum({
                    filePath: url,
                    success: function(data) {
                        wx.hideLoading();
                        wx.showToast({
                            title: "保存图片成功"
                        });
                    },
                    fail: function(err) {
                        wx.hideLoading();
                        if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
                            wx.showToast({
                                title: "请允许访问相册权限",
                                icon: 'none'
                            })
                            _this.setData({
                                isAuthSetting:true
                            })
                        }
                    }
                })
            }
        })
    }
})